package zut.edu.cs.network.io;

import java.io.File;
import java.io.IOException;

public class TraverseFile {
	
	public void traverseFile(String pathName, int depth) throws IOException {
		
		File file = new File(pathName);
		if (!file.exists()) {
			System.out.println("路径不存在！");
			return;
		}
		if ((!file.isDirectory()) && file.isFile()) {
			System.out.println("不是目录，是文件！"+file.getName());
			return;
		}
		
		for (int i = 0; i < depth; i++) {
			System.out.print("  ");
		}
		System.out.print("|--");
		System.out.println(file.getName());
		
		String[] fileList = file.list();
		int currentDepth=depth+1;
		for (String string : fileList) {
			File file1 = new File(file.getPath(),string);
			if (file1.isDirectory()) {
//			   递归遍历
				traverseFile(file1.getCanonicalPath(),currentDepth);
			}else {
				for (int i = 0; i < currentDepth; i++) {
					System.out.print("  ");
				}
				System.out.print("|--");
				System.out.println(file1.getName());
			}
		}
		
	}

}
